import tools from '@/shop/utils/tools'
import {router} from '@/router';
import http from '@/config/requestConfig'
import api from '@/config/baseRequest'
export const state = {
	//用户数据
	userInfo: {},
	// 用户订单
	userOrders: {},
	// token信息
	AuthToken: '',
	// 是否显示登录弹窗
	showLoginTip: false,
	shareInfo: {
		title: '[石榴趣购]和我一起拼团，领商家补贴，提现无门槛秒到账',
		path: `pages/index/index`,
		image: '',
	} // 默认分享数据
};
export const mutations = {
	// 修改token
	setToken(state, data) {
		console.log("存储token", data);
		if (data) {
			state.AuthToken = data;
			// #ifdef H5
			// uni.setStorageSync('AuthToken', data);
			window.sessionStorage.setItem('AuthToken', data);
			// #endif
			// #ifndef H5
			uni.setStorageSync('AuthToken', data);
			// window.sessionStorage.setItem('AuthToken',  data);
			// #endif
			// #ifdef APP-PLUS
			uni.setStorageSync('AuthToken', data);
			// #endif

		}
	},
	//储存用户信息
	setUserInfo(state, data) {
		if (data) {
			state.userInfo = Object.assign({}, state.userInfo, data);
			console.log(state.userInfo);
			// #ifdef H5
			window.sessionStorage.setItem('userInfo', JSON.stringify(state.userInfo));
			// #endif
			// #ifndef H5
			uni.setStorageSync('userInfo', state.userInfo);
			// #endif
		}
	},
	//储存用户信息
	setUserOrders(state, data) {
		if (data) {
			state.userOrders = Object.assign({}, state.userOrders, data);
			console.log(state.userOrders);
			// #ifdef H5
			window.sessionStorage.setItem('userOrders', JSON.stringify(state.userOrders));
			// #endif
			// #ifndef H5
			uni.setStorageSync('userOrders', state.userOrders);
			// #endif
		}
	},
	// 修改用户分享信息
	shareInfo(state, shareInfo) {
		state.shareInfo = shareInfo;
	},
	// 修改登录弹窗
	['LOGIN_TIP'](state, data) {
		state.showLoginTip = data
	},
	// 退出APP
	emptyUserInfo(state) {
		state.userInfo = {};
		state.userOrders = {};
		state.AuthToken = '';
		// #ifdef H5
		window.sessionStorage.removeItem("userInfo");
		window.sessionStorage.removeItem("AuthToken");
		window.sessionStorage.removeItem("userOrders");
		uni.removeStorageSync("AuthToken");
		uni.removeStorageSync("userInfo");
		uni.removeStorageSync("userOrders");
		// #endif
		// #ifndef H5
		uni.removeStorageSync("userInfo");
		uni.removeStorageSync("AuthToken");
		uni.removeStorageSync("userOrders");
		// #endif
	},
};
export const actions = {
	//设置token并返回上次页面
	setTokenAndBack({
		commit
	}, token) {
		uni.setStorageSync('token', token);
		let fromLogin = uni.getStorageSync('fromLogin');
		
		if (fromLogin) {
			// tools.routerTo(fromLogin.path, fromLogin.query, true);
			router.replaceAll({path: fromLogin.path, query: fromLogin.query})
			uni.removeStorageSync('fromLogin')
		} else {
			//默认跳转首页S
			router.replaceAll('/pages/index/index')
		}
	},
	// 获取用户个人信息
	getUserPrefile({
		state,
		commit
	}) {
		api.get('/addons/yun_shop/api.php?route=member.member.mappmember-data').then(res => {
			commit('setUserInfo', res.data.member);
			commit('setUserOrders', res.data.order);
			commit('setToken', tools.getCookieValueByKey("PHPSESSID"));
		})
	}
};

export const getters = {
	shareInfo: state => state.shareInfo
}